package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.InvestmentData;
import org.example.mapper.InvestmentDataMapper;
import org.example.service.InvestmentDataService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 投资数据表Service实现类
 */
@Service
public class InvestmentDataServiceImpl extends ServiceImpl<InvestmentDataMapper, InvestmentData>
        implements InvestmentDataService {

    @Override
    public int batchSave(List<InvestmentData> dataList) {
        if (dataList == null || dataList.isEmpty()) {
            return 0;
        }

        // 使用MyBatis Plus的批量保存
        return this.saveBatch(dataList) ? dataList.size() : 0;
    }

    @Override
    public List<InvestmentData> getByPeriodDate(String periodDate) {
        LambdaQueryWrapper<InvestmentData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InvestmentData::getPeriodDate, periodDate)
                .orderByAsc(InvestmentData::getCreatedTime);
        return this.list(wrapper);
    }

    @Override
    public int clearByPeriodDate(String periodDate) {
        LambdaQueryWrapper<InvestmentData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(InvestmentData::getPeriodDate, periodDate);
        return this.baseMapper.delete(wrapper);
    }

    @Override
    public List<InvestmentData> getAllData() {
        LambdaQueryWrapper<InvestmentData> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(InvestmentData::getPeriodDate)
                .orderByAsc(InvestmentData::getCreatedTime);
        return this.list(wrapper);
    }

    @Override
    public List<InvestmentData> getByPeriodRange(String startPeriod, String endPeriod) {
        LambdaQueryWrapper<InvestmentData> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(InvestmentData::getPeriodDate, startPeriod)
                .le(InvestmentData::getPeriodDate, endPeriod)
                .orderByAsc(InvestmentData::getPeriodDate)
                .orderByAsc(InvestmentData::getCreatedTime);
        return this.list(wrapper);
    }
}
